/**
 * 文件处理
 */
CommonUtils.regNamespace("file", "index");

file.index = (function(){
	//文件上传
	var _uploadFile = function(){
		//获取参数
		var apkName = $("#apkName").val();
		if(apkName=="" || apkName==null){
			alert("请输入apk名称");
			return;
		}
		var version = $("#version").val();
		if(version==""||version==null){
			alert("请输入apk版本号");
			return;
		}
		var apkDesc = $("#apkDesc").val();
		var params = {
			"apkName":apkName,
			"version":version,
			"apkDesc":apkDesc
		};
		var path = contextPath+"/file/uploadFile";
		$.ajaxFileUpload({
		            url: path,
		            type:'post',
		            fileElementId:"apkFile",
		            secureuri: false,
		            dataType:"json",
		            async:false,
		            beforeSend:function(){
			        	$.ecOverlay("正在上传中...");
			        },
		            success:function(data,status)	{
		                //上传成功返回的信息｛filePath:上传文件后取得的url，result:是否成功，成功为true，失败为false｝
		            	if(data){
		            		var savePath = data.savePath;
		            		var fileName = data.fileName;
		            		var localApkPath = data.localApkPath;
		            		params.savePath = savePath;
		            		params.fileName = fileName;
		            		params.localApkPath = localApkPath;
		            		file.index.insertApkInfo(params);
		            	}
		            	$.unecOverlay();
		            },
		            error:function(data,status,e){
		                alert("上传异常:"+e);
		                $.unecOverlay();
		            }
		        }
		    );
	};
	//文件上传
	var _uploadDocFile = function(){
		//获取参数
		var documentName = $("#documentName").val();
		if(documentName=="" || documentName==null){
			alert("请输入文件名称");
			return;
		}
		var documentVersion = $("#documentVersion").val();
		if(documentVersion==""||documentVersion==null){
			alert("请输入文件版本号");
			return;
		}
		var documentDesc = $("#documentDesc").val();
		var params = {
			"documentName":documentName,
			"documentVersion":documentVersion,
			"documentDesc":documentDesc
		};
		var path = contextPath+"/file/uploadDocFile";
		$.ajaxFileUpload({
		            url: path,
		            type:'post',
		            fileElementId:"docFile",
		            secureuri: false,
		            dataType:"json",
		            async:false,
		            beforeSend:function(){
			        	$.ecOverlay("正在上传中...");
			        },
		            success:function(data,status)	{
		                //上传成功返回的信息｛filePath:上传文件后取得的url，result:是否成功，成功为true，失败为false｝
		            	if(data){
		            		var savePath = data.savePath;
		            		var fileName = data.fileName;
		            		var localDocPath = data.localDocPath;
		            		params.savePath = savePath;
		            		params.fileName = fileName;
		            		params.localDocPath = localDocPath;
		            		params.documentVersion = documentVersion;
		            		params.documentDesc = documentDesc;
		            		file.index.insertDocument(params);
		            	}
		            	$.unecOverlay();
		            },
		            error:function(data,status,e){
		                alert("上传异常:"+e);
		                $.unecOverlay();
		            }
		        }
		    );
	};
	
	var _insertDocument = function(params){
		var path= contextPath+"/manage/insertDocument";
		$.ajax({
	        type:"POST",
	        url:path,
	        data:JSON.stringify(params),
	        contentType:"application/json; charset=utf-8",
	        dataType: "json",
	        async:false,
	        success:function(respone){
	        	if(respone){
	        		if(respone.code==0){
	        			Lobibox.alert('success', {
	    		            msg: "上传文件成功"
	    		        });
	        			$("#uploadDocumentModal").modal("hide");
	        			//重新刷新文件信息
	        			project.index.queryTotalDocument();
	        		}else{
	        			Lobibox.alert('error', {
	                        msg: respone.message
	                    });
	        		}
	        	}else{
	        		alert("上传文件失败");
	        	}
	        },error:function(respone){
	            alert("请求发生错误:"+respone);
	        }
	    });
	};

	var _insertApkInfo = function(params){
		var path= contextPath+"/apk/insertApkInfo";
		$.ajax({
	        type:"POST",
	        url:path,
	        data:JSON.stringify(params),
	        contentType:"application/json; charset=utf-8",
	        dataType: "json",
	        async:false,
	        success:function(respone){
	        	if(respone){
	        		if(respone.code==0){
	        			Lobibox.alert('success', {
	    		            msg: "上传apk成功"
	    		        });
	        			$("#uploadFileModal").modal("hide");
	        			//重新刷新apk信息
	        			//window.location.href = contextPath + "/apk/totalApk";
	        			project.index.queryTotalApkInfo();
	        		}else{
	        			Lobibox.alert('error', {
	                        msg: respone.message
	                    });
	        		}
	        	}else{
	        		alert("上传apk失败");
	        	}
	        },error:function(respone){
	            alert("请求发生错误:"+respone);
	        }
	    });
	};
	return {
		uploadFile:_uploadFile,
		insertApkInfo:_insertApkInfo,
		uploadDocFile:_uploadDocFile,
		insertDocument:_insertDocument
	};
	
})();